<template>
  <Header :title="{ zh: '冲裁目视管理看板', en: 'Cutting & Preparation Visual Board', index: 9 }" />

  <div class="EchartsContainer">
    <div class="row" style="height: 50%">
      <div class="item" style="overflow: hidden">
        <div class="title" style="text-align: start">日生产状况</div>
        <el-table ref="tab" border class="table" :data="listTable" style="width: 100%; z-index: 0">
          <el-table-column align="center" prop="deptName" label="部门" width="100px" />
          <el-table-column align="center" prop="inQty" label="产量" />
          <el-table-column align="center" prop="deptPpl" label="人数" />
          <el-table-column align="center" prop="pplReq" label="人均产量" width="90px" />
          <el-table-column align="center" prop="stkQty" label="库存数" />
          <el-table-column align="center" prop="matchQty" label="配套数" />
          <el-table-column align="center" prop="matchPercent" label="配套率" />
        </el-table>
        <!-- <div
          style="
            background-image: url('../oil.svg');
            background-repeat: no-repeat;
            background-position: 50% 40%;
            background-size: cover;
            height: 215px;
            opacity: 0.7;
          "
        ></div> -->
      </div>
    </div>

    <div class="row" style="margin-top: 10px; margin-bottom: 20px">
      <div class="item" style="width: 70%">
        <div class="title" style="margin-bottom: 20px">月发料状况(双)</div>
        <div class="echarts" id="main3"></div>
      </div>
      <div class="item" style="width: 30%; padding: 0; margin-left: 10px">
        <div class="title" style="margin-bottom: 20px">月产能状况</div>
        <div class="echarts" id="main2"></div>
      </div>
      <!-- <swiper :autoplay="{
        delay: 60000
      }" :slides-per-view="1" :space-between="2000" style="height: 100%; width: 60vw">
        <swiper-slide>
          <div class="item" style="width: 100%; padding: 0; margin-left: 10px">
            <div class="title">周产能状况(双)</div>
            <div class="echarts" id="main"></div>
          </div>
        </swiper-slide>
        <swiper-slide>
          
        </swiper-slide>
      </swiper> -->
    </div>
  </div>
</template>

<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import Header from '../components/Header.vue'
import * as echarts from 'echarts'
import { OutsoleInventory } from '@/api/Nine'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { Swiper, SwiperSlide } from 'swiper/vue'

let rollTimer = ref() as any
const tab = ref<HTMLElement>() as any
const autoRoll = (stop: any) => {
  if (stop) {
    clearInterval(rollTimer.value)
    return
  }
  const divData = tab.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  rollTimer.value = setInterval(() => {
    divData.scrollTop += 1
    if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
      divData.scrollTop = 0
    }
  }, 150)
}
onMounted(() => {
  autoRoll(false)
})
onBeforeUnmount(() => {
  autoRoll(true)
})

onMounted(async () => {
  await getListTable()
  await WeekCapacity()
  await MonthCapacity()
  getEchartsDom()
})

const MonthSendingAndReceiving = async () => {
  // 月 产能状况
  try {
    let qerM = (await request.post<any, any>(`/show/prod/blank/stats`, {
      endDate: '',
      pushUnit: '',
      titleUnit: '当月'
    })) as any
    const cDMAP = new Map(
      qerM.map((item: any) => [item.type + item.title.split('/')[1], item.count])
    )
    chartData.value.forEach((item: any, index: number) => {
      item.CtyQty.forEach((item2: any, index2: number) => {
        if (cDMAP.get('冲裁' + Number(index2 + 1))) {
          chartData.value[index].CtyQty[index2] = cDMAP.get('冲裁' + Number(index2 + 1))
        } else {
          chartData.value[index].CtyQty[index2] = '0'
        }
      })
      item.PreQty.forEach((item2: any, index2: number) => {
        if (cDMAP.get('配套' + Number(index2 + 1))) {
          chartData.value[index].PreQty[index2] = cDMAP.get('配套' + Number(index2 + 1))
        } else {
          chartData.value[index].PreQty[index2] = '0'
        }
      })
    })
  } catch (error: any) {
    ElMessage.error(error)
  }
}

const DeptNos = ref([]) as any

const MonthCapacity = async () => {
  // 获取月生产状况
  try {
    let qerM = (await request.post<any, any>(`/show/prod/blank/stats`, {
      endDate: '',
      pushUnit: '1',
      titleUnit: '月'
    })) as any
    const c3MAP = new Map(qerM.map((item: any) => [item.type, item.count]))
    showData.value.chart3.data = [c3MAP.get('冲裁')]
    showData.value.chart3.data2 = [c3MAP.get('配套')]
  } catch (error: any) {
    ElMessage.error(error)
  }
}

const showData = ref({
  chart2: {
    data: [
      {
        name: '星期一',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [7602, 8198]
      },
      {
        name: '星期二',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [5603, 9177]
      },
      {
        name: '星期三',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [17535, 9494]
      },
      {
        name: '星期四',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [3037, 6638]
      },
      {
        name: '星期五',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [0, 0]
      },
      {
        name: '星期六',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [0, 0]
      },
      {
        name: '星期日',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [62165, 58425]
      }
    ]
  },
  chart3: {
    data: [],
    data2: []
  }
}) as any

const chartData = ref([
  {
    CtyQty: [
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
      27, 28, 29, 30, 31
    ],
    PreQty: [
      31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8,
      7, 6, 5, 4, 3, 2, 1
    ]
  }
]) as any

const MonthDay = ref<any>([])

const WeekCapacity = async () => {
  // 周 产能 状况(双)
  try {
    let qerW = (await request.post<any, any>(`/show/prod/blank/stats`, {
      endDate: '',
      pushUnit: '1',
      titleUnit: '本周'
    })) as any
    const c2MAP = new Map(qerW.map((item: any) => [item.type + item.title, item.count]))
    showData.value.chart2.data.forEach((item: any) => {
      if (c2MAP.get('冲裁' + item.name) && c2MAP.get('配套' + item.name)) {
        item.data = [c2MAP.get('冲裁' + item.name), c2MAP.get('配套' + item.name)]
      }
    })
  } catch (error: any) {
    ElMessage.error(error)
  }
}

const listTable = ref([])
const getListTable = async () => {
  try {
    let tableDataList = (await request.get(`/show/prod/blank/cpc`)) as any
    console.log(tableDataList)
    listTable.value = tableDataList.map((item: any) => {
      return {
        deptName: item.deptName,
        inQty: item.prodQty,
        deptPpl: item.person,
        pplReq: item.personProd,
        stkQty: item.stkQty,
        matchQty: item.match,
        matchPercent: item.matchPercent
      }
    })
  } catch (error: any) {
    ElMessage.error(error)
  }
}
// 表格数据

const getEchartsDom = () => {
  let app = {} as any
  const posList = [
    'left',
    'right',
    'top',
    'bottom',
    'inside',
    'insideTop',
    'insideLeft',
    'insideRight',
    'insideBottom',
    'insideTopLeft',
    'insideTopRight',
    'insideBottomLeft',
    'insideBottomRight'
  ]
  app.configParameters = {
    rotate: {
      min: -90,
      max: 90
    },
    align: {
      options: {
        left: 'left',
        center: 'center',
        right: 'right'
      }
    },
    verticalAlign: {
      options: {
        top: 'top',
        middle: 'middle',
        bottom: 'bottom'
      }
    },
    position: {
      options: posList.reduce(function (map: any, pos) {
        map[pos] = pos
        return map
      }, {})
    },
    distance: {
      min: 0
      // max: 100
    }
  }
  app.config = {
    rotate: 90,
    align: 'left',
    verticalAlign: 'middle',
    position: 'insideBottom',
    distance: 15,
    onChange: function () {
      const labelOption = {
        rotate: app.config.rotate,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        position: app.config.position,
        distance: app.config.distance
      }
      myChart.setOption({
        series: [
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          }
        ]
      })
    }
  }
  // 第4张图
  let chartDom2 = document.getElementById('main2')
  let myChart2 = echarts.init(chartDom2)
  let option2 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    legend: {
      data: ['冲裁', '配套'],
      top: 'bottom'
    },
    grid: {
      left: 50,
      right: 40,
      top: 20,
      bottom: 55
    },
    xAxis: [
      {
        type: 'category',
        data: MonthDay.value,
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        // max: 20,
        // interval: 5,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        name: '冲裁',
        type: 'bar',
        barWidth: '15%',
        label: {
          show: true,
          position: 'top'
        },
        tooltip: {
          valueFormatter: function (value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#FAC959' },
              { offset: 1, color: '#FFFEFA' }
            ]
          )
        },
        //data: [42298]
        data: showData.value.chart3.data
      },
      {
        name: '配套',
        type: 'bar',
        barWidth: '15%',
        label: {
          show: true,
          position: 'top'
        },
        tooltip: {
          valueFormatter: function (value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#88ADFB' },
              { offset: 1, color: '#E3ECFF' }
            ]
          )
        },
        //data: [39319]
        data: showData.value.chart3.data2
      }
    ]
  }
  option2 && myChart2.setOption(option2)

  // 第2张图
  let chartDom3 = document.getElementById('main3')
  let myChart3 = echarts.init(chartDom3)
  let option3 = {
    legend: {
      data: ['当月冲裁实发', '当月配套实发'],
      top: 'bottom'
    },
    grid: {
      left: 50,
      right: 20,
      top: 20,
      bottom: 50
    },
    xAxis: {
      type: 'category',
      data: [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
        26, 27, 28, 29, 30, 31
      ]
    },
    yAxis: {
      type: 'value'
      //max: 12000
    },
    tooltip: {
      trigger: 'axis'
    },
    series: [
      {
        name: '当月冲裁实发',
        //data: [9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000],
        data: chartData.value[0].CtyQty,
        type: 'line',
        label: {
          show: true,
          position: 'top'
        },
        color: '#91CC75'
      },
      {
        name: '当月配套实发',
        //data: [0, 9882, 10231, 11350, 10825, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        data: chartData.value[0].PreQty,
        type: 'line',
        label: {
          show: true,
          position: 'top'
        },
        color: '#FAC858'
      }
    ]
  }
  option3 && myChart3.setOption(option3)
}

onMounted(() => {
  function One() {
    if (location.href.indexOf('#reloaded') == -1) {
      location.href = location.href + '#reloaded'
      location.reload()
    }
  }
  One()
  MonthSendingAndReceiving()
})
</script>

<style scoped lang="less">
.EchartsContainer {
  border-radius: 4px;
  margin-top: 10px;
  width: 100%;
  height: calc(100vh - 7vh - 40px);
}

.row {
  display: flex;
  height: 50%;
  width: 100%;

  .item {
    background-color: white;
    width: 100%;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding-bottom: 10px;
    padding: 0 10px 5px;

    // align-items: center;
    .title {
      font-weight: 900;
      font-size: 1rem;
      margin-top: 10px;
      text-align: center;
      color: black;
    }

    .echarts {
      flex: 1;
    }
  }
}

:deep(thead) {
  .is-center {
    color: white;
    background-color: #ff9940;
  }
}
</style>
